/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.userManager;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UpdateUser extends HttpServlet {

	public String DRIVER, URL, USER, PASS;        

        public void init() throws ServletException {
        ServletContext context = getServletContext();
        DRIVER = context.getInitParameter("driver");
	URL = context.getInitParameter("dburl");
        USER = context.getInitParameter("dbuser");
        PASS = context.getInitParameter("dbpass");
        }


	public void doGet (HttpServletRequest request,
                            HttpServletResponse response)
				throws ServletException, IOException {
	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	Connection con = null;

	try {	                
                Class.forName(DRIVER);
		con = DriverManager.getConnection(URL,USER,PASS);
		
		String query = 
                "UPDATE adminpersonnel SET " +
                "FirstName = '" + request.getParameter("firstName") + "', " +
                "LastName = '" + request.getParameter("lastName") + "', " +
                "Phone = '" + request.getParameter("phone") + "', " +
                "Email = '" + request.getParameter("email") + "', " +
                "UserID = '" + request.getParameter("username") + "', " +
                "Password = '" + request.getParameter("password") + "'" +
                "WHERE AdminID = '" + request.getParameter("AdminID") + "'";
                
                PreparedStatement stmt = con.prepareStatement(query);
              
                int result = stmt.executeUpdate(query);
              
                response.sendRedirect(request.getParameter("redirectURL"));
        
} catch (SQLException ex) {
	System.out.println("\nERROR:------ SQLException -----\n");
        while (ex != null) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode :" + ex.getErrorCode());
            ex = ex.getNextException();
        }
    } catch (Exception e) {
        e.printStackTrace();        
} finally {
    try {	
    if (con != null)
        con.close();
	} catch (SQLException ex) {
            System.out.println("\nERROR:------ SQLException -----\n");
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode :" + ex.getErrorCode());
        }
            }
    }
    public void doPost(HttpServletRequest request,
                                HttpServletResponse response)
          throws ServletException, IOException {
              doGet(request, response);
        }
}